package org.i1510.common.utils.file;
import java.io.File;
import java.io.IOException;

import javax.activation.FileTypeMap;
import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletRequest;


public class FileMimeTypeUtil {
	private static FileTypeMap defaultMap = null;

	public static String getContentType(File file,HttpServletRequest request) {
		String filename = file.getName();
		return getContentType(filename,request);
	}

	public static String getContentType(String filename,HttpServletRequest request) {
		if (defaultMap == null) {
			try {
				defaultMap = new MimetypesFileTypeMap(request.getSession().getServletContext().getRealPath("/")+ "/META-INF/mime.types");
			} catch (IOException e) {
			}
		}
		return defaultMap.getContentType(filename.toLowerCase());

	}
}